<!DOCTYPE html>

<html lang="en">
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>国际化（Internationalization） &mdash; Phalcon 2.0.0 文档</title>
    <meta name="keywords" content="php, phalcon, phalcon php, php framework, faster php framework"><link rel="stylesheet" type="text/css" href="../_static/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="http://static.phalconphp.com/css/phalcon.min.css" />
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400' rel='stylesheet' type='text/css'>
    <link href="http://fonts.googleapis.com/css?family=Merriweather:400,700" rel="stylesheet" type="text/css" />
    <!--
    EUROPE <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400&subset=latin-ext' rel='stylesheet' type='text/css'>
    GREEK <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400&subset=greek-ext' rel='stylesheet' type='text/css'>
    RUSSIA <link href='https://fonts.googleapis.com/css?family=Open+Sans:700,400&subset=cyrillic-ext,latin' rel='stylesheet' type='text/css'>
    -->

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/docs.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.0.0',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>

    <script src="../_static/jquery.min.js"></script>
    <script type="text/javascript" src="../_static/docs.js"></script>
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
    <link rel="top" title="Phalcon 2.0.0 文档" href="../index.html" />
    <link rel="next" title="数据库迁移（Database Migrations）" href="migrations.html" />
    <link rel="prev" title="数据库抽象层（Database Abstraction Layer）" href="db.html" /> 
  </head>
  <body>

<header class="page-header">
    <nav class="navbar" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-menu-container">
                    <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
                </button>
                <a class="navbar-brand phalcon-logo" href="/"><span itemprop="name" class="sr-only">Phalcon PHP</span></a>
            </div>

            <div class="collapse navbar-collapse navbar-right" id="main-menu-container">
                <ul class="nav navbar-nav main-menu">
                  <li class="first"><a href="http://phalconphp.com/en/download" class="header-nav-link">Download</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                  <li><a href="http://forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                  <li><a href="http://blog.phalconphp.com/" class="header-nav-link" target="_blank">Blog</a></li>
                  <li><a href="http://phalconphp.com/en/about">About</a></li>
                </ul>
            </div>
        </div>
    </nav>
  </header>

<div class="heading">
    <div class="container">
        <div class="row">
            <h2>Documentation</h2>
        </div>
    </div>
</div>
    <!--<div class="header-line">
      <div class="size-wrap">
        <div class="header-line-title title-white">Documentation</div>
      </div>
    </div>-->
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="migrations.html" title="数据库迁移（Database Migrations）"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="db.html" title="数据库抽象层（Database Abstraction Layer）"
             accesskey="P">上一页</a> |</li>
        <li><a href="http://phalconphp.com">Home</a> &raquo;</li>
        <li><a href="../index.html">Phalcon 2.0.0 文档</a> &raquo;</li> 
      </ul>
    </div>  

      <table width="100%" align="center" cellpadding="0" cellspacing="0">
        <tr>
      <td class="primary-box" width="25%" valign="top">
            <div>
            <div id="searchbox" style="">
                <!--<form class="search" action="http://readthedocs.org/search/project/" method="get">
                  <input type="search" name="q" size="25" placeholder="Search">
                  <input type="submit" value="Go">
                  <input type="hidden" name="selected_facets" value="project:">
                </form>-->
                <div style="width:200px;padding:10px">
                  <gcse:searchbox-only></gcse:searchbox-only>
                </div>
            </div>
            </div>
            <div style="padding:5px;padding-left:10px">
              <div id="carbonads-container">
                <div class="carbonad"><div id="azcarbon"></div>
                <script type="text/javascript">var z = document.createElement("script"); z.type = "text/javascript"; z.async = true; z.src = "http://engine.carbonads.com/z/56496/azcarbon_2_1_0_VERT"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(z, s);</script>
                </div></div>
            </div>
            <h3><a href="../index.html">內容目录</a></h3>
            <ul>
<li><a class="reference internal" href="#">国际化（Internationalization）</a><ul>
<li><a class="reference internal" href="#find-out-best-available-locale">匹配最佳的区域设置（Find out best available Locale）</a></li>
<li><a class="reference internal" href="#formatting-messages-based-on-locale">基于区域设置格式化信息（Formatting messages based on Locale）</a></li>
<li><a class="reference internal" href="#locale-sensitive-comparison">特定区域设置的字符串比较（Locale-Sensitive comparison）</a></li>
<li><a class="reference internal" href="#transliteration">音译（Transliteration）</a></li>
</ul>
</li>
</ul>

            <h4>上一个主题</h4>
            <p class="topless"><a href="db.html" title="上一章">&lt; 数据库抽象层（Database Abstraction Layer）</a></p>
            <h4>下一个主题</h4>
            <p class="topless"><a href="migrations.html" title="下一章">数据库迁移（Database Migrations） &gt;</a></p>
            <h3>本页</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/reference/intl.txt" rel="nofollow">显示源代码</a></li>
            </ul>
        </td>
          <td class="second-box" valign="top">
            <div class="document">
                <div class="documentwrapper">
                  <div class="bodywrapper">
                    <div class="body" >
                      
  <div class="section" id="internationalization">
<h1>国际化（Internationalization）<a class="headerlink" href="#internationalization" title="永久链接至标题">¶</a></h1>
<p>Phalcon is written in C as an extension for PHP. There is a <a class="reference external" href="http://pecl.php.net/package/intl">PECL</a> extension that offers internationalization functions to PHP applications called <a class="reference external" href="http://pecl.php.net/package/intl">intl</a>.
Starting from PHP 5.4/5.5 this extension is bundled with PHP. Its documentation can be found in the pages of the official <a class="reference external" href="http://www.php.net/manual/en/intro.intl.php">PHP manual</a>.</p>
<p>Phalcon does not offer this functionality, since creating such a component would be replicating existing code.</p>
<p>In the examples below, we will show you how to implement the <a class="reference external" href="http://pecl.php.net/package/intl">intl</a> extension&#8217;s functionality into Phalcon powered applications.</p>
<blockquote class="highlights">
<div>This guide is not intended to be a complete documentation of the <a class="reference external" href="http://pecl.php.net/package/intl">intl</a> extension. Please visit its the <a class="reference external" href="http://www.php.net/manual/en/book.intl.php">documentation</a> of the extension for a reference.</div></blockquote>
<div class="section" id="find-out-best-available-locale">
<h2>匹配最佳的区域设置（Find out best available Locale）<a class="headerlink" href="#find-out-best-available-locale" title="永久链接至标题">¶</a></h2>
<p>There are several ways to find out the best available locale using <a class="reference external" href="http://pecl.php.net/package/intl">intl</a>. One of them is to check the HTTP &#8220;Accept-Language&#8221; header:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="nv">$locale</span> <span class="o">=</span> <span class="nx">Locale</span><span class="o">::</span><span class="na">acceptFromHttp</span><span class="p">(</span><span class="nv">$_SERVER</span><span class="p">[</span><span class="s2">&quot;HTTP_ACCEPT_LANGUAGE&quot;</span><span class="p">]);</span>

<span class="c1">// Locale could be something like &quot;en_GB&quot; or &quot;en&quot;</span>
<span class="k">echo</span> <span class="nv">$locale</span><span class="p">;</span>
</pre></div>
</div>
<p>Below method returns a locale identified. It is used to get language, culture, or regionally-specific behavior from the Locale API.
Examples of identifiers include:</p>
<ul class="simple">
<li>en-US (English, United States)</li>
<li>zh-Hant-TW (Chinese, Traditional Script, Taiwan)</li>
<li>fr-CA, fr-FR (French for Canada and France respectively)</li>
</ul>
</div>
<div class="section" id="formatting-messages-based-on-locale">
<h2>基于区域设置格式化信息（Formatting messages based on Locale）<a class="headerlink" href="#formatting-messages-based-on-locale" title="永久链接至标题">¶</a></h2>
<p>Part of creating a localized application is to produce concatenated, language-neutral messages. The <a class="reference external" href="http://www.php.net/manual/en/class.messageformatter.php">MessageFormatter</a> allows for the
production of those messages.</p>
<p>Printing numbers formatted based on some locale:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="c1">// Prints € 4 560</span>
<span class="nv">$formatter</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MessageFormatter</span><span class="p">(</span><span class="s2">&quot;fr_FR&quot;</span><span class="p">,</span> <span class="s2">&quot;€ {0, number, integer}&quot;</span><span class="p">);</span>
<span class="k">echo</span> <span class="nv">$formatter</span><span class="o">-&gt;</span><span class="na">format</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="mi">4560</span><span class="p">));</span>

<span class="c1">// Prints USD$ 4,560.5</span>
<span class="nv">$formatter</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MessageFormatter</span><span class="p">(</span><span class="s2">&quot;en_US&quot;</span><span class="p">,</span> <span class="s2">&quot;USD$ {0, number}&quot;</span><span class="p">);</span>
<span class="k">echo</span> <span class="nv">$formatter</span><span class="o">-&gt;</span><span class="na">format</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="mf">4560.50</span><span class="p">));</span>

<span class="c1">// Prints ARS$ 1.250,25</span>
<span class="nv">$formatter</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MessageFormatter</span><span class="p">(</span><span class="s2">&quot;es_AR&quot;</span><span class="p">,</span> <span class="s2">&quot;ARS$ {0, number}&quot;</span><span class="p">);</span>
<span class="k">echo</span> <span class="nv">$formatter</span><span class="o">-&gt;</span><span class="na">format</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="mf">1250.25</span><span class="p">));</span>
</pre></div>
</div>
<p>Message formatting using time and date patterns:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="c1">//Setting parameters</span>
<span class="nv">$time</span>   <span class="o">=</span> <span class="nb">time</span><span class="p">();</span>
<span class="nv">$values</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="nv">$time</span><span class="p">,</span> <span class="nv">$time</span><span class="p">);</span>

<span class="c1">// Prints &quot;At 3:50:31 PM on Apr 19, 2012, there was a disturbance on planet 7.&quot;</span>
<span class="nv">$pattern</span>   <span class="o">=</span> <span class="s2">&quot;At {1, time} on {1, date}, there was a disturbance on planet {0, number}.&quot;</span><span class="p">;</span>
<span class="nv">$formatter</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MessageFormatter</span><span class="p">(</span><span class="s2">&quot;en_US&quot;</span><span class="p">,</span> <span class="nv">$pattern</span><span class="p">);</span>
<span class="k">echo</span> <span class="nv">$formatter</span><span class="o">-&gt;</span><span class="na">format</span><span class="p">(</span><span class="nv">$values</span><span class="p">);</span>

<span class="c1">// Prints &quot;À 15:53:01 le 19 avr. 2012, il y avait une perturbation sur la planète 7.&quot;</span>
<span class="nv">$pattern</span>   <span class="o">=</span> <span class="s2">&quot;À {1, time} le {1, date}, il y avait une perturbation sur la planète {0, number}.&quot;</span><span class="p">;</span>
<span class="nv">$formatter</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MessageFormatter</span><span class="p">(</span><span class="s2">&quot;fr_FR&quot;</span><span class="p">,</span> <span class="nv">$pattern</span><span class="p">);</span>
<span class="k">echo</span> <span class="nv">$formatter</span><span class="o">-&gt;</span><span class="na">format</span><span class="p">(</span><span class="nv">$values</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="locale-sensitive-comparison">
<h2>特定区域设置的字符串比较（Locale-Sensitive comparison）<a class="headerlink" href="#locale-sensitive-comparison" title="永久链接至标题">¶</a></h2>
<p>The <a class="reference external" href="http://www.php.net/manual/en/class.collator.php">Collator</a> class provides string comparison capability with support for appropriate locale-sensitive sort orderings. Check the
examples below on the usage of this class:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="c1">// Create a collator using Spanish locale</span>
<span class="nv">$collator</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Collator</span><span class="p">(</span><span class="s2">&quot;es&quot;</span><span class="p">);</span>

<span class="c1">// Returns that the strings are equal, in spite of the emphasis on the &quot;o&quot;</span>
<span class="nv">$collator</span><span class="o">-&gt;</span><span class="na">setStrength</span><span class="p">(</span><span class="nx">Collator</span><span class="o">::</span><span class="na">PRIMARY</span><span class="p">);</span>
<span class="nb">var_dump</span><span class="p">(</span><span class="nv">$collator</span><span class="o">-&gt;</span><span class="na">compare</span><span class="p">(</span><span class="s2">&quot;una canción&quot;</span><span class="p">,</span> <span class="s2">&quot;una cancion&quot;</span><span class="p">));</span>

<span class="c1">// Returns that the strings are not equal</span>
<span class="nv">$collator</span><span class="o">-&gt;</span><span class="na">setStrength</span><span class="p">(</span><span class="nx">Collator</span><span class="o">::</span><span class="na">DEFAULT_VALUE</span><span class="p">);</span>
<span class="nb">var_dump</span><span class="p">(</span><span class="nv">$collator</span><span class="o">-&gt;</span><span class="na">compare</span><span class="p">(</span><span class="s2">&quot;una canción&quot;</span><span class="p">,</span> <span class="s2">&quot;una cancion&quot;</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="transliteration">
<h2>音译（Transliteration）<a class="headerlink" href="#transliteration" title="永久链接至标题">¶</a></h2>
<p><a class="reference external" href="http://www.php.net/manual/en/class.transliterator.php">Transliterator</a> provides transliteration of strings:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="nv">$id</span> <span class="o">=</span> <span class="s2">&quot;Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();&quot;</span><span class="p">;</span>
<span class="nv">$transliterator</span> <span class="o">=</span> <span class="nx">Transliterator</span><span class="o">::</span><span class="na">create</span><span class="p">(</span><span class="nv">$id</span><span class="p">);</span>

<span class="nv">$string</span> <span class="o">=</span> <span class="s2">&quot;garçon-étudiant-où-L&#39;école&quot;</span><span class="p">;</span>
<span class="k">echo</span> <span class="nv">$transliterator</span><span class="o">-&gt;</span><span class="na">transliterate</span><span class="p">(</span><span class="nv">$string</span><span class="p">);</span> <span class="c1">// garconetudiantoulecole</span>
</pre></div>
</div>
</div>
</div>


                    </div>
                  </div>
                </div>
            </div>
          </td>
        </tr>
      </table>
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="migrations.html" title="数据库迁移（Database Migrations）"
             >下一页</a> |</li>
        <li class="right" >
          <a href="db.html" title="数据库抽象层（Database Abstraction Layer）"
             >上一页</a> |</li> 
      </ul>
    </div>

      <div class="prefooter">
  <div class="container">
      <div class="row">
          <div class="col-sm-3 text-right">
              <span>Follow along:</span>
          </div>
          <div class="col-sm-6 text-center">
              <a href="https://twitter.com/phalconphp" alt="Twitter" class="btn-social btn-social-twitter"><i class="icon-twitter"></i></a>
              <a href="https://www.facebook.com/pages/Phalcon-Framework/134230726685897" alt="Facebook" class="btn-social btn-social-facebook"><i class="icon-facebook"></i></a>
              <a href="https://plus.google.com/102376109340560896457" alt="Google+" class="btn-social btn-social-googleplus"><i class="icon-googleplus"></i></a>
              <a href="https://github.com/phalcon/cphalcon" alt="Github" class="btn-social btn-social-github"><i class="icon-github"></i></a>
          </div>
          <div class="col-sm-3">
          </div>
      </div>
  </div>

</div>
<footer class="footer">
  <div class="container">
      <div class="row">
          <div class="col-xs-4 col-sm-3">
              <h4>Download</h4>
              <ul>
                  <li><a href="http://phalconphp.com/download">Installing Phalcon</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                  <li><a href="http://api.phalconphp.com">API</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html">Tutorial</a></li>
                  <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html#sample-applications">Sample Applications</a></li>
              </ul>
          </div>
          <div class="col-xs-4 col-sm-3">
              <h4>Community</h4>
              <ul>
                  <li><a href="http://forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                  <li><a href="https://github.com/phalcon/cphalcon">GitHub</a></li>
                  <li><a href="https://github.com/phalcon/cphalcon/issues">Issue Tracker</a></li>
                  <li><a href="http://stackoverflow.com/questions/tagged/phalcon">Stack Overflow</a></li>
                  <li><a href="http://phalconphp.com/en/testimonials">Testimonials</a></li>
                  <li><a href="http://builtwith.phalconphp.com/">Built with Phalcon</a></li>
                  <li><a href="http://store.phalconphp.com/">Store</a></li>
              </ul>
          </div>
          <div class="col-xs-4 col-sm-2">
              <h4>About</h4>
              <ul>
                  <li><a class="link-black" href="http://blog.phalconphp.com/">Blog</a></li>
                  <li><a href="http://phalconphp.com/en/about">About</a></li>
                  <li><a href="http://phalconphp.com/en/team">Team</a></li>
                  <li><a href="http://phalconphp.com/en/roadmap">Roadmap</a></li>
                  <li><a href="http://phalconphp.com/en/donate">Donate</a></li>
                  <li><a href="http://phalconphp.com/en/consulting">Consulting</a></li>
                  <li><a href="http://phalconphp.com/en/hosting">Hosting</a></li>
              </ul>
          </div>
          <div id="license-spaccer" class="visible-xs"></div>
          <div id="license-wrapper" class="col-xs-12 col-sm-4">
              <p class="license">

                  Found a typo or an error? Want to improve this document? The documentation sources are available on <a href="http://github.com/phalcon/docs">Github</a><br>
                  Need support or have questions? Check our <a href="http://forum.phalconphp.com">Forum</a><br>
                  <br>

                  Phalcon Framework is released under the <a href="https://github.com/phalcon/cphalcon/blob/master/docs/LICENSE.md">new BSD license</a>.<br>
                  Except where otherwise noted, content on this site is licensed under the
                    <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 License.</a>

                <div class="design">
                  <span>Designed by:</span>

                  <a href="http://www.fog-city.net/" class="fogcity" target="_blank" title="Fog City Software"><span>Fog City Software</span></a>
              </div>
          </div>
      </div>
  </div>
</footer>

    </div>
    <script type="text/javascript">
    $(window).on("load", function(){
      var cx = '009733439235723428699:lh9ltjgvdz8';
      var gcse = document.createElement('script');
      gcse.type = 'text/javascript';
      gcse.async = true;
      gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(gcse, s);
    });
    </script>

  </body>
</html>